﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class BuildTestEditor : Editor
{
    const BuildTarget TestBuildTarget = BuildTarget.StandaloneOSX;
    const string AssetBundlesOutputPath = "Assets/OutPut";
    const int BundleCount = 3;

    const string MatPath = "Assets/Contents/Rock.mat";
    const string PrefabPath = "Assets/Contents/Cube.prefab";
    const string ScenePath = "Assets/Scenes/test.unity";


    private static void ClearPathFolder()
    {
        FileUtil.DeleteFileOrDirectory(AssetBundlesOutputPath);
        AssetDatabase.CreateFolder("Assets", "OutPut");
    }

    [MenuItem("BuildTest/BuildAssetBundle_3_Res_Prefab_Scene")]
    public static void BuildAssetBundle_Res_Prefab_Scene()
    {
        ClearPathFolder();

        AssetBundleBuild[] abbuilds = new AssetBundleBuild[BundleCount];
        for(int i=0; i<abbuilds.Length; i++)
        {
            abbuilds[i] = new AssetBundleBuild();
        }
        // 1. material resource assetbundle build
        abbuilds[0].assetBundleName = "resource";


        string[] depends = AssetDatabase.GetDependencies(MatPath);

        for (int i = 0; i < depends.Length; i++)
        {
            Debug.Log("Material depends " + i + " :" + depends[i]);
        }
        abbuilds[0].assetNames = depends;

        // 2. prefab assetbundle build
        abbuilds[1].assetBundleName = "prefab";
        depends = AssetDatabase.GetDependencies(PrefabPath);
        for (int i = 0; i < depends.Length; i++)
        {
            Debug.Log("Prefab depends " + i + " :" + depends[i]);
        }
        abbuilds[1].assetNames = new string[]{ PrefabPath };

        // 3. scene assetbundle build
        abbuilds[2].assetBundleName = "scene";
        depends = AssetDatabase.GetDependencies(ScenePath);
        for (int i = 0; i < depends.Length; i++)
        {
            Debug.Log("Scene depends " + i + " :" + depends[i]);
        }
        abbuilds[2].assetNames = new string[] { ScenePath };

        BuildPipeline.BuildAssetBundles(AssetBundlesOutputPath, abbuilds, 0, TestBuildTarget);

        AssetDatabase.Refresh();
    }

    [MenuItem("BuildTest/BuildAssetBundle_2_Prefab_Scene")]
    public static void BuildAssetBundle_Prefab_Scene()
    {
        ClearPathFolder();

        AssetBundleBuild[] abbuilds = new AssetBundleBuild[BundleCount - 1];
        for (int i = 0; i < abbuilds.Length; i++)
        {
            abbuilds[i] = new AssetBundleBuild();
        }
        // 1. prefab assetbundle build
        abbuilds[0].assetBundleName = "prefab";
        string[] depends = AssetDatabase.GetDependencies(PrefabPath);

        for (int i = 0; i < depends.Length; i++)
        {
            Debug.Log("Prefab depends " + i + " :" + depends[i]);
        }
        //remove prefab self path
        List<string> list = new List<string>(depends);
        list.RemoveAt(0);
        abbuilds[0].assetNames = list.ToArray();

        // 2. scene assetbundle build
        abbuilds[1].assetBundleName = "scene";
        depends = AssetDatabase.GetDependencies(ScenePath);
        for (int i = 0; i < depends.Length; i++)
        {
            Debug.Log("Scene depends " + i + " :" + depends[i]);
        }
        abbuilds[1].assetNames = new string[] { ScenePath, PrefabPath };

        BuildPipeline.BuildAssetBundles(AssetBundlesOutputPath, abbuilds, 0, TestBuildTarget);

        AssetDatabase.Refresh();
    }
}
